<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\BookController;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\FanqieApiController;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
    return $request->user();
});

// 健康检查接口
Route::get('/health', function () {
    return response()->json([
        'status' => 'healthy',
        'message' => '服务运行正常',
        'timestamp' => now(),
        'server_time' => date('Y-m-d H:i:s'),
        'php_version' => PHP_VERSION
    ]);
});

// 认证相关路由
Route::prefix('auth')->group(function () {
    // 用户注册
    Route::post('/register', [AuthController::class, 'register']);
    
    // 用户登录
    Route::post('/login', [AuthController::class, 'login']);
    
    // 需要认证的路由
    Route::middleware('auth:sanctum')->group(function () {
        // 用户登出
        Route::post('/logout', [AuthController::class, 'logout']);
        
        // 获取当前用户信息
        Route::get('/user', [AuthController::class, 'user']);
    });
});

// 书籍管理 API 路由 (保留原始的BookController路由)
Route::prefix('books')->group(function () {
    // 获取所有书籍列表
    Route::get('/', [BookController::class, 'index']);
    
    // 创建新书籍
    Route::post('/', [BookController::class, 'store']);
    
    // 搜索书籍
    Route::get('/search', [BookController::class, 'search']);
    
    // 获取单个书籍详情
    Route::get('/{id}', [BookController::class, 'show']);
    
    // 更新书籍信息
    Route::put('/{id}', [BookController::class, 'update']);
    
    // 删除书籍
    Route::delete('/{id}', [BookController::class, 'destroy']);
});

// 番茄小说 API 路由
Route::prefix('fanqie')->group(function () {
    // 获取所有书籍列表
    Route::get('/list', [FanqieApiController::class, 'index']);
    
    // 创建新书籍
    Route::post('/', [FanqieApiController::class, 'store']);
    
    // 搜索书籍
    Route::get('/search', [FanqieApiController::class, 'search']);
    
    // 获取单个书籍详情
    Route::get('/{id}', [FanqieApiController::class, 'show']);
    
    // 更新书籍信息
    Route::put('/{id}', [FanqieApiController::class, 'update']);
    
    // 删除书籍
    Route::delete('/{id}', [FanqieApiController::class, 'destroy']);
    
    // 上传图片
    Route::post('/upload', [FanqieApiController::class, 'upload']);
    
});